package com.alphathink.qrcodecard.Activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alphathink.slidemenu.SlideMenu;
import com.alphathink.xpg.qrcodecard.R;
import com.zxing.activity.CaptureActivity;

/**
 * 使用二维码与名片结合<br>
 * 可扫描别人的二维码并实现获取名片
 * 
 * @author xpg
 * 
 */

public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";

	private SlideMenu mSlideMenu;

	private ImageView mImageView, menuImg;// 添加的图片
	private EditText mName, mPhoneNum, mQQNum, mWeiBo;
	private Button mOKBtn, mCancleBtn;

	private TextView mScanTextView, mSlideScanTV;// 侧边栏的扫一扫
	private ButtonListener mButtonListener = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		getWindow().setFeatureInt(Window.FEATURE_NO_TITLE,
				R.layout.layout_title_bar);

		System.out.println("sfdfsdfsd");
		initView();// 初始化视图组件

		mButtonListener = new ButtonListener();
		mOKBtn.setOnClickListener(mButtonListener);
		mCancleBtn.setOnClickListener(mButtonListener);

		mSlideScanTV.setOnClickListener(mButtonListener);
	}

	private void initView() {
		View view = View.inflate(getApplicationContext(),
				R.layout.layout_title_bar, null);

		mSlideMenu = (SlideMenu) findViewById(R.id.slide_menu);
		System.out.println(TAG + "   mSlideMenu-->" + mSlideMenu);
		menuImg = (ImageView) findViewById(R.id.title_bar_menu_btn);

		mImageView = (ImageView) findViewById(R.id.main_ImageView);
		mScanTextView = (TextView) view
				.findViewById(R.id.title_bar_menu_TV_Scan);

		mName = (EditText) findViewById(R.id.main_NameET);
		mPhoneNum = (EditText) findViewById(R.id.main_PhoneET);
		mQQNum = (EditText) findViewById(R.id.main_qqET);
		mWeiBo = (EditText) findViewById(R.id.main_SinaET);

		mOKBtn = (Button) findViewById(R.id.main_OKBtn);
		mCancleBtn = (Button) findViewById(R.id.main_CancleBtn);

		mSlideScanTV = (TextView) mSlideMenu
				.findViewById(R.id.slide_menu_TV_ScanTV);
		System.out.println(TAG + "       mSlideMenuTV--->" + mSlideScanTV);
	}

	class ButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			switch (v.getId()) {
			// title中的扫一扫TextView
			case R.id.title_bar_menu_TV_Scan:
				intent.setClass(MainActivity.this, CaptureActivity.class);
				startActivityForResult(intent, 0);
				break;
			// SlideMenu中的扫一扫textView
			case R.id.slide_menu_TV_ScanTV:
				Log.d(TAG, " Scan  sdfsdf");
				intent.setClass(getApplicationContext(), CaptureActivity.class);
				startActivityForResult(intent, 0);
				break;
			case R.id.main_OKBtn:
				Log.e(TAG, "点击了确定!");
				Toast.makeText(getApplication(), TAG, Toast.LENGTH_SHORT)
						.show();
				String type = "0";
				String message = "成功";
				JSONObject qRCode = new JSONObject();
				StringBuffer stringBuffer = new StringBuffer();
				stringBuffer.append("{" + "\"type\"" + ":" + "\"" + type + "\""
						+ "," + "\"message\"" + ":" + "\"" + message + "\""
						+ "," + "\"data\"" + ":" + "[");
				stringBuffer.append("{" + "\"userName\"" + ":" + "\""
						+ mName.getText().toString() + "\"" + ","
						+ "\"userPhoneNum\"" + ":" + "\""
						+ mPhoneNum.getText().toString() + "\"" + ","
						+ "\"userQQNum\"" + ":" + "\""
						+ mQQNum.getText().toString() + "\"" + ","
						+ "\"userWeiBo\"" + ":" + "\""
						+ mWeiBo.getText().toString() + "\"" + "}");
				stringBuffer.append("]");
				stringBuffer.append("}");
				System.out.println(stringBuffer.toString());
				try {
					qRCode = new JSONObject(stringBuffer.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}

				intent.putExtra("data", stringBuffer.toString());
				intent.setClass(MainActivity.this, QRCode.class);
				startActivity(intent);
				break;
			case R.id.main_CancleBtn:
				mPhoneNum.setText("");
				mWeiBo.setText("");
				mName.setText("");
				mQQNum.setText("");
				break;
			case R.id.title_bar_menu_btn:
				System.out.println("danle");

				if (mSlideMenu.isMainScreenShowing()) {
					mSlideMenu.openMenu();
				} else {
					mSlideMenu.closeMenu();
				}
				break;

			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub

		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == CaptureActivity.CAP_RESULT) {
			String str = data.getExtras().getString("result");
			Log.d(TAG, "onActivityResult--->扫描之后的返回的数据:" + str);

			Intent intent = new Intent();
			intent.putExtra("data", str);
			intent.setClass(MainActivity.this, ScanQRResult.class);
			startActivity(intent);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		System.out.println("kanwo ");
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// /noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}